package com.siqi.hr.common.utils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {

	public static boolean hasValue(Object o) {
		if ((o == null) || (o.toString().trim().equals(""))) {
			return false;
		}
		return true;
	}

    /**
	 * 拼接sql in条件后面的条件
	 * @author   xiaogq
	 * @param exportDataArray
	 * @return
     */
	public static String getString(String exportDataArray) {
		if ((exportDataArray != null) && (!exportDataArray.trim().equals(""))) {
			StringBuilder entityIds = new StringBuilder();
			String[] ids = exportDataArray.split(",");
			for (int i = 0; i < ids.length; i++) {
				entityIds.append("'" + ids[i] + "',");
			}
			entityIds.deleteCharAt(entityIds.lastIndexOf(","));
			return entityIds.toString();
		}
		return exportDataArray;
	}

	/**
	 * 正则验证字符串中是否包含中文字符
	 * @author   xioagq
	 * @param dateStr   日期时间字符串
	 * @return the parsed date
	 */
	public static boolean hasChinese(String dateStr){
		dateStr = new String(dateStr.getBytes());
		String pattern = "[\u4e00-\u9fa5]+";
		Pattern p = Pattern.compile(pattern);
		Matcher result = p.matcher(dateStr);
		//是否含有中文字符
		boolean bool = result.find();
		return bool;
	}

    /**
	 * 获取服务器项目地址(格式：http://ip:端口号/项目名)
	 * @param request
	 * @return
     */
	public static String getServiceUrl(HttpServletRequest request){
		String port = request.getServerPort() == 80 ? "" : ":"+request.getServerPort();
		String url = request.getScheme()+"://"+request.getServerName()+port+request.getContextPath();
		return url;
	}

	/**
	 * 导出cvs格式文件时去除换行符
	 * @xiaogq
	 * @param str
	 * @return
     */
	public static String replaceBlankMoreForExport(String str) {
		String dest = "";
		if (str != null) {
			Pattern p = Pattern.compile("\r|\n");
			Matcher m = p.matcher(str);
			dest = m.replaceAll("");
		}
		return dest;
	}

	public static void main(String[] args) {
	}
}
